export const json2Arr = (json) => {
    let cardInfo = {
        key: ['companyname', 'cardId', 'empnoId', 'name', 'Department', 'post'],
        value: ['机构', '卡号', '编号', '姓名', '年级/部门', '班级/职务']
    };
    let arr = [];
    try {
        for (let i = 0; i < cardInfo.key.length; i++) {
            let obj = {
                key: cardInfo.value[i],
                value: json[cardInfo.key[i]] || ''
            };
            arr.push(obj);
        }
    } catch (e) {
        return arr;
    }
    return arr;
};

/**
 * 给指定数字补零
 * @param num 数字
 * @param n 补零后的长度
 */
export const addZero = (num, n) => {
    let arr = [];
    let len = n - num.toString().length;
    if (len <= 0) return num;
    if (!arr[len]) arr[len] = (new Array(len + 1)).join('0');
    return arr[len] + num;
};

/**
 * 获取指定格式时间
 * @param date
 * @param format
 */
export const getFormatDate = (date = new Date(), format = 'YYYY-MM-DD') => {
    let year = date.getFullYear();
    let month = addZero(date.getMonth() + 1, 2);
    let day = addZero(date.getDate(), 2);
    switch (format) {
        case 'YYYY-MM':
            return year + '-' + month;
        case 'yyyyMM':
            return year + '' + month;

        case 'YYYY-MM-DD':
            return year + '-' + month + '-' + day;

        case 'yyyyMMdd':
            return year + '' + month + '' + day;

        default:
            return year + '-' + month + '-' + day;
    }

};

//计算指定月份的月初和月末
export const getMonthDay = (date) => {
    let firstDay = new Date(date.split('-')[0] + '/' + date.split('-')[1] + '/' + '01 00:00:00');
    firstDay.setDate(1);
    let lastDay = new Date(+firstDay);
    lastDay.setMonth(lastDay.getMonth() + 1);
    lastDay.setDate(0);
    return [getFormatDate(firstDay), getFormatDate(lastDay)];
};

/**
 * 获取指定日期的后几天
 * @param days
 * @param date
 */
export const getDateAfterDay = (days, date = new Date()) => {
    let newDate = new Date();
    newDate.setDate(date.getDate() + days);
    return getFormatDate(newDate);
};

/**
 * 去掉日期的-
 * @param date
 */
export const translateDate = (date) => {
    return date.split('-').join('');
};

export const jsonArr2Arr = (jsonArr, key) => {
    let arr = [];
    for (let i = 0; i < jsonArr.length; i++) {
        arr.push(jsonArr[i][key]);
    }
    return arr;
};

export const getJsonKeyByKey = (jsonArr, key1, key2, key2Value) => {
    let key1Value = '';
    for (let i = 0; i < jsonArr.length; i++) {
        if (jsonArr[i][key2] === key2Value) {
            key1Value = jsonArr[i][key1];
            break;
        }
    }
    return key1Value;
};

export const timePipe = (timeStr, format = 'YYYY-MM-DD') => {
    let len = timeStr ? timeStr.length : 0;
    switch (len) {
        case 8:
            return format === 'YYYY-MM-DD'
                ? timeStr.slice(0, 4) + '-' + timeStr.slice(4, 6) + '-' + timeStr.slice(6, 8)
                : timeStr.slice(4, 6) + '-' + timeStr.slice(6, 8);
        case 14:
            return timeStr.slice(0, 4) + '-' + timeStr.slice(4, 6) + '-' + timeStr.slice(6, 8) + ' ' + timeStr.slice(8, 10) + ':' + timeStr.slice(10, 12) + ':' + timeStr.slice(12, 14);
        case 0:
        default:
            return timeStr;
    }
};

export const timeWeekPipe = (timeStr) => {
    let year = timeStr.slice(0, 4);
    let month = timeStr.slice(4, 6);
    let day = timeStr.slice(6, 8);
    const date = new Date(`${year}-${month}-${day}`);
    const arr = ['日', '一', '二', '三', '四', '五', '六']
    return `${month}月${day}日(星期${arr[date.getDay()]})`;
};

export const delay = (timeout) => {
    return new Promise(resolve => {
        setTimeout(resolve, timeout);
    });
};

export const json2String = (json) => {
    let str = '';
    for (const key in json) {
        if (json.hasOwnProperty(key)) {
            str += key + '=' + json[key] + '&';
        }
    }
    return str.slice(0, -1);
};
